package service;

import com.distribute.pojo.Result;
import com.distribute.pojo.enums.ServiceType;
import com.distribute.pojo.project.Project;
import com.distribute.utils.JsonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import constant.DSConstants;
import core.api.ApiClient;
import exception.ApiException;

import service.api.TaskService;

import java.util.List;

public class DSTaskService implements TaskService {

    private final ApiClient apiClient;

    public DSTaskService(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    /**
     * 生成 task Code
     * @param projectCode
     * @param num
     * @return
     */
    @Override
    public List<Long> genTaskCodes(Long projectCode, Integer num) {
        if(projectCode == null || num < 1){
            return null;
        }
        Result result = apiClient.get(DSConstants.TASK_CODE_GEN, Result.class, projectCode, num);
        if (result.isFailed()) {
            throw new ApiException(result.getMsg());
        }
        String content = JsonUtils.converString(result.getData());
        List<Long> codes = JsonUtils.toList(content, new TypeReference<List<Long>>() {
        });
        return codes;
    }

    @Override
    public ServiceType getServiceType() {
        return ServiceType.TASK;
    }
}
